package database.helpers;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

@SuppressLint({ "ParserError", "ParserError" })
public class CaringFriend_Table
{
		private databas_helper dbHelper;	
		private SQLiteDatabase database;
			/**
			 * Assignment table related constants.
			 */
			public final static String CARINGFRIEND_TABLE="CARINGFRIEND";	
			public  static String CARINGFRIEND_NAME="name";
			public final static String CARINGFRIEND_PENDINGQN="PENDINGQN";	
			public final static String CARINGFRIEND_CELLNUMBER="cellnumber";
			public final static String CARINGFRIEND_EMAIL="email";	
			public final static String CARINGFRIEND_ID="_id";			

			/**
			 * 
			 * @param context
			 */
			public CaringFriend_Table(Context context){
				dbHelper = new databas_helper(context);
				database = dbHelper.getWritableDatabase();
			}
			public CaringFriend_Table(databas_helper dbHelper,SQLiteDatabase database1){
				this.dbHelper =dbHelper ;
				this.database = database1;
			}
			
			/**\
			 * Creates a new CARINGFRIEND
			 	 */
			public long addCARINGFRIEND(String name,String PENDINGQN, String cellnumber, String email){
				ContentValues values = new ContentValues();
				values.put(CARINGFRIEND_NAME,name);
				values.put(CARINGFRIEND_PENDINGQN,PENDINGQN);
				values.put(CARINGFRIEND_CELLNUMBER,cellnumber);
				values.put(CARINGFRIEND_EMAIL,email);
				return database.insert(CARINGFRIEND_TABLE, null, values);
			}	
			
			public void close() {
				dbHelper.close();
			}
			/**
			 * Fetch all CARINGFRIENDs
			 * @return
			 */
			public Cursor getAllCARINGFRIENDs(){
				Cursor mCursor = database.query(true, CARINGFRIEND_TABLE, new String[]
						{
						CARINGFRIEND_ID,CARINGFRIEND_NAME,CARINGFRIEND_PENDINGQN,CARINGFRIEND_CELLNUMBER,CARINGFRIEND_EMAIL},null
						, null, null, null, null, null);
				if (mCursor != null) {
					mCursor.moveToFirst();
				}
				return mCursor;
			}
			public Cursor getCelNumbyID(String id)
			{		

		        String selectQuery = "SELECT * FROM " + CARINGFRIEND_TABLE + " WHERE " +CARINGFRIEND_PENDINGQN + "="+id; 
		        database = dbHelper.getReadableDatabase();
		        Cursor cursor = database.rawQuery(selectQuery, null);
		        return cursor;
			}
			
			public Cursor getCARINGFRIENDById(String id)
			{		

		        String selectQuery = "SELECT * FROM " + CARINGFRIEND_TABLE + " WHERE " + CARINGFRIEND_NAME + "='"+id+"'"; 
		       
		        Cursor cursor = database.rawQuery(selectQuery, null);
		        return cursor;
			}
			public boolean getCARINGFRIENDByPENDINGQNSTATUS(String id)
			{		

		        String selectQuery = "SELECT * FROM " + CARINGFRIEND_TABLE + " WHERE " + CARINGFRIEND_PENDINGQN + "='"+id+"'"; 
		       
		        Cursor cursor = database.rawQuery(selectQuery, null);
		        if(!cursor.isAfterLast())
		        {
		        	cursor.close();
		        return true;
		        }
		    	cursor.close();
		        return false;
		        
			}
			
			public int editCARINGFRIEND(long care_id, String name,String PENDINGQN, String cellnumber, String email, String relationship ,String img_url){
				ContentValues values = new ContentValues();
				values.put(CARINGFRIEND_NAME,name);
				values.put(CARINGFRIEND_PENDINGQN,PENDINGQN);
				values.put(CARINGFRIEND_CELLNUMBER,cellnumber);
				values.put(CARINGFRIEND_EMAIL,email);
				return database.update(CARINGFRIEND_TABLE, values, CARINGFRIEND_ID + "=" + care_id, null) ;
				
//				return database.update(CARINGFRIEND_TABLE, values,CARINGFRIEND_ID + "=?",new String[]{name,PENDINGQN,cellnumber,email,relationship});
			}
			
			public boolean delete(String pname){
				return database.delete(CARINGFRIEND_TABLE, CARINGFRIEND_NAME +"='"+pname+"'", null) > 0;
			}
			
			public Cursor getHighPENDINGQN(){
				int max=1;
				Cursor cursor =getAllCARINGFRIENDs();
						if(!cursor.isAfterLast())
						{
							cursor.moveToFirst();
						}
						while(!cursor.isAfterLast())
						{
							if(Integer.parseInt(cursor.getString(2))>max)
							{
							max=Integer.parseInt(cursor.getString(2));
							}
							cursor.moveToNext();
						}
				cursor.close();
						
				String selectQuery = "SELECT * FROM " + CARINGFRIEND_TABLE + " WHERE " + CARINGFRIEND_PENDINGQN + "='"+max+"'"; 			       
		        Cursor cursor1 = database.rawQuery(selectQuery, null);
		        return cursor1;
			}


			public void deleteAll() {
				
				database.delete(CARINGFRIEND_TABLE, null, null);
				
			}
}


